//
//  SucceedToChiefViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/4.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "SucceedToChiefViewController.h"
#import "JoinRoomPopViewController.h"
#import "MainIndexViewController.h"
#import "MainNavigationViewController.h"

#import <TSMessages/TSMessageView.h>
#import <MobClick.h>

#import "User.h"
#import "House.h"
#import "Seat.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIButton+WebCache.h>
#import "ACMacros.h"
#import "Constants.h"
#import "HttpServer.h"

#import "AppDrawer.h"
#import "WXApi.h"

@interface SucceedToChiefViewController ()

@end

@implementation SucceedToChiefViewController {
    WXMediaMessage *shareMessage;
    NSString *shareUrl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureViewTitle];
    [self loadTeamData];
    [self loadShareData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBarHidden = true;
    [MobClick beginLogPageView:@"成功创建小组页"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBarHidden = false;
    [MobClick endLogPageView:@"成功创建小组页"];
}

- (void)viewDidAppear:(BOOL)animated {
    [self configureAlerts];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)configureAlerts {
    [TSMessage showNotificationInViewController:self title:@"申请成功!" subtitle:@"创建小组成功" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
}

- (void)configureViewTitle {
    User *user = [User sharedUser];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:user.avatar]];
    if ([user.gender isEqual:@"f"]) {
        [self.genderImage setImage:[UIImage imageNamed:@"boy_22_22"]];
    } else {
        [self.genderImage setImage:[UIImage imageNamed:@"girl_22_22"]];
    }
    [self.infoLabel setText:[NSString stringWithFormat:@"%d后 %@",user.ageGroup, user.constellation]];
    [self.proInfoLabel setText:[NSString stringWithFormat:@"%@ - %@",user.industry, user.profession]];
    if (user.certification) {
        [self.checkImage setImage:[UIImage imageNamed:@"check_ID-44-36"]];
        for (UILabel *label in self.checkLabel) {
            [label setTextColor:RGBACOLOR(255, 118, 108, 1)];
        }
    } else {
        [self.checkImage setImage:[UIImage imageNamed:@"check_ID_dim-44-36"]];
        for (UILabel *label in self.checkLabel) {
            [label setTextColor:RGBACOLOR(160, 160, 160, 1)];
        }
    }
    if (user.enterpriseAuth) {
        [self.companyAuthImages setImage:[UIImage imageNamed:@"check_company"]];
        for (UILabel *label in self.companyAuthLabels) {
            [label setTextColor:RGBACOLOR(255, 118, 108, 1)];
        }
    } else {
        [self.companyAuthImages setImage:[UIImage imageNamed:@"check_company_dim"]];
        for (UILabel *label in self.companyAuthLabels) {
            [label setTextColor:RGBACOLOR(160, 160, 160, 1)];
        }
    }
    for (int i = 0; i < user.tags.count; i++) {
        if (user.tags.count <= self.tagsLabels.count) {
            UILabel *label = self.tagsLabels[i];
            [label setText:user.tags[i]];
        }
    }

}


- (void)configureTeam {
    Team *team = self.team;
    if (self.team == nil) {
        self.houseView.hidden = true;
    } else {
        [self.houseImage sd_setImageWithURL:[NSURL URLWithString:team.houseCover]];
        [self.houseNameLabel setText:team.houseCommunity];

        [self.houseCommunityLabel setText:team.houseCommunity];
        [self.houseCountLabel setText:[NSString stringWithFormat:@"| %d室%d厅%d卫 |",team.houseRoomNum, team.houseHallNum, team.houseToiletNum]];
        [self.houseAreaLabel setText:[NSString stringWithFormat:@"%dm²",team.houseArea]];
        int lowerPrice = 0;
        int UpperPrice = 0;
        NSArray *labels = @[self.houseALabel, self.houseBLabel, self.houseCLabel];
        for (int i = 0; i < team.seats.count; i++) {
            Seat *seat = [[Seat alloc]initWithDic:team.seats[i]];
            if (i == 0) {
                lowerPrice = seat.rent;
                UpperPrice = seat.rent;
            } else {
                if (seat.rent > UpperPrice) {
                    UpperPrice = seat.rent;
                }
                if (seat.rent < lowerPrice) {
                    lowerPrice = seat.rent;
                }
            }
            if (seat.leader) {
                UILabel *roomLabel = labels[i];
                [roomLabel setText:[NSString stringWithFormat:@"%@:室长承包",seat.roomName]];
            } else {
                UILabel *roomLabel = labels[i];
                [roomLabel setText:[NSString stringWithFormat:@"%@:%d元",seat.roomName, seat.rent]];
            }
        }
        [self.housePriceLabel setText:[NSString stringWithFormat:@"¥%d 元 - ¥%d 元",lowerPrice, UpperPrice]];
    }
    
    if (team.houseCheck) {
    } else {
        self.houseCheckImage.hidden = true;
        self.houseCheckLabel.hidden = true;
    }
}

- (IBAction)clickToPopCancel:(UIButton *)sender {
    [[AppDrawer sharedDrawer].centerViewController dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (IBAction)shareButtonClicked:(UIButton *)sender {
    SendMessageToWXReq *shareReq;
    shareReq = [[SendMessageToWXReq alloc]init];
    shareReq.bText = false;
    shareReq.message = shareMessage;
    WXWebpageObject *webObj = [[WXWebpageObject alloc]init];
    webObj.webpageUrl = shareUrl;
    shareReq.message.mediaObject = webObj;
    shareReq.scene = 1;
    [WXApi sendReq:shareReq];
}

- (WXMediaMessage *)wechateShareData:(NSDictionary *)successDic {
    WXMediaMessage *message = [[WXMediaMessage alloc]init];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:[successDic objectForKey:@"thumbnail"]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        message.thumbData = data;
    }];
    message.title = [successDic objectForKey:@"title"];
    message.description = [successDic objectForKey:@"description"];
    return message;
}

#pragma mark - Network methods
- (void)loadShareData {
    if (self.teamId == nil || [self.teamId isEqual:@""]) {
    } else {
        [HttpServer getSharelinksWithType:@"team" sharedInfo:self.teamId WithSuccessBlock:^(NSDictionary *successDic) {
            shareUrl = [successDic objectForKey:@"link"];
            shareMessage = [self wechateShareData:successDic];
        } withFailedBlock:^(id failData) {
            NSLog(@"%@",failData);
        }];
    }
}

- (void)loadTeamData {
    [HttpServer getTeamWithId:self.teamId WithSuccessBlock:^(NSDictionary *successDic) {
        Team *team = [[Team alloc]initWithDic:[successDic objectForKey:@"team"]];
        self.team = team;
        [self configureTeam];
    } withFailBlock:^(id failData) {
        NSLog(@"%@",failData);
    }];
}

@end
